package com.sphd.restaurantsystem.model;


import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.info.GreensInfo;
import com.sphd.restaurantsystem.info.SortInfo;
import com.sphd.restaurantsystem.info.VipInfo;
import com.sphd.restaurantsystem.interfaces.VolleyJsonObjectCallBack;
import com.sphd.restaurantsystem.presenter.WaiterPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/11/22
 */

public class WaiterModelImpl implements WaiterModel, VolleyJsonObjectCallBack {
    WaiterPresenterImpl waiterPresenter;

    @Override
    public void sendUrl(String url, WaiterPresenterImpl waiterPresenter) {
        this.waiterPresenter = waiterPresenter;
        volleyUtil.setJsonObjectRequest(url, this);
    }

    @Override
    public void sendUserurl(String url, WaiterPresenterImpl waiterPresenter) {
        this.waiterPresenter = waiterPresenter;
        volleyUtil.setJsonObjectRequest(url, new VolleyJsonObjectCallBack() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getString("res").equals("0")) {
                        waiterPresenter.sendUerError();
                    } else if (response.getString("res").equals("-1")) {
                        waiterPresenter.sendUerError();
                    } else {
                        List<VipInfo> vipInfos = JsonUtil.getListFromString(response.getString("res"),VipInfo.class);
                        waiterPresenter.sendUer(vipInfos);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            waiterPresenter.sendError(error);
            }
        });
    }



    @Override
    public void onResponse(JSONObject response) {
        try {
            List<SortInfo> sortInfos = JsonUtil.getListFromString(response.getString("sorts"), SortInfo.class);
            List<GreensInfo> greensInfos = JsonUtil.getListFromString(response.getString("dishes"), GreensInfo.class);

            waiterPresenter.senRes(sortInfos, greensInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {

    }
}